package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBorrower;
import cn.stylefeng.guns.sys.modular.system.entity.FileMsg;
import cn.stylefeng.guns.sys.modular.system.service.BtyBorrowerFlowInformationService;
import cn.stylefeng.guns.sys.modular.system.service.BtyBorrowerService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 借贷款人信息控制器
 *
 * @author liuy
 * @Date
 */
@Controller
@RequestMapping("/btyBorrower")
public class BtyBorrowerController extends BaseController {

    private String PREFIX = "/modular/system/btyBorrower/";
    @Resource
    private BtyBorrowerFlowInformationService borrowerFlowInformationService;
    @Resource
    private BtyBorrowerService btyBorrowerService;
    private String userName = "admin";

    /**
     * 是否是督导的标志off
     */
    private String off = "off";

    /**
     * 跳转到通知列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyBorrower.html";
    }

    /**
     * 获取通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition, String tel) {
        //获取用户的帐号
        Long userId = LoginContextHolder.getContext().getUserId();
        String username = LoginContextHolder.getContext().getUser().getName();
        Page<Map<String, Object>> list = this.btyBorrowerService.list(condition,tel);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 跳转到添加档案
     */
    @RequestMapping("/btyBorrowerFlowInfo_add")
    public String noticeAdd() {
        return PREFIX + "btyBorrowerFlowInfo_add.html";
    }

    /**
     * 修改借贷款人信息
     *
     * @author liuy
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BtyBorrower btyBorrower) {
        if (ToolUtil.isOneEmpty(btyBorrower, btyBorrower.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
       String tel=btyBorrower.getTel().replaceAll("\\s*", "");
        btyBorrower.setTel(tel);
        this.btyBorrowerService.updateById(btyBorrower);
        return SUCCESS_TIP;
    }

    /**
     * 新增借贷款人信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestParam String name, @RequestParam String tel) {
        if(ToolUtil.isNotEmpty(tel)){
            //替换大部分空白字符,不限于空格
            tel=tel.replaceAll("\\s*", "");
        }
        //添加之前判断手机号是否存在
        BtyBorrower btyBorrowers=btyBorrowerService.getOne(new QueryWrapper<BtyBorrower>().eq("tel",tel));
        if(ToolUtil.isNotEmpty(btyBorrowers)){
            return 1;
        }else {
            BtyBorrower btyBorrower = new BtyBorrower();
            btyBorrower.setName(name);
            btyBorrower.setTel(tel);
            btyBorrower.setCreateUser(LoginContextHolder.getContext().getUserId());
            btyBorrower.setCreateTime(new Date());
            this.btyBorrowerService.save(btyBorrower);
            return SUCCESS_TIP;
        }
    }

    /**
     * 删除借贷款人信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long id) {
        this.btyBorrowerService.removeById(id);
        return SUCCESS_TIP;
    }

    //获取本地文件夹下名称
    @RequestMapping(value = "/file")
    @ResponseBody
    public List<FileMsg> file() {
        File file = new File("D:\\pictureUpload\\project\\model");
        File[] fileList = file.listFiles();
        List<FileMsg> files = new ArrayList<>();
        for (File fileName : fileList) {
            FileMsg fileMsg = new FileMsg();
            fileMsg.setName(fileName.getName());
            fileMsg.setUrl(fileName.getAbsolutePath());
            fileMsg.setSize(fileName.length() + "");
            files.add(fileMsg);
        }
        return files;
    }

//    /**
//     * 获取借款人
//     */
//    @RequestMapping(value = "/borrowerInfo")
//    @ResponseBody
//    public List<BtyBorrowerFlowInformation> borrowerInfo(){
//        Long userId= LoginContextHolder.getContext().getUserId();
//        //获取用户的帐号
//        String account= LoginContextHolder.getContext().getUser().getName();
//        List<String> roleNames= LoginContextHolder.getContext().getUser().getRoleNames();
//        List<BtyBorrowerFlowInformation> list=borrowerFlowInformationService.borrowerInfo();
//
//        return list;
//    }

}
